<?php session_start(); ?>
<!DOCTYPE html>
<html>
    <head>
        <link rel="stylesheet" type="text/css" href="../css/projectstyle.css">

        <!-- php script used to delete the relation between user and course-->
        <?php
        if ($_SERVER["REQUEST_METHOD"] == "POST") {
            $user = $_SESSION['user'];
            $password = $_SESSION['password'];
            $database = $_SESSION['database'];
            $host = $_SESSION['host'];
            $condb = mysqli_connect($host, $user, $password, $database);

            if (isset($_POST['kurser'])) {

                $COURSEID = $_POST['kurser'];
                $delete = "delete from PERSON_TAKES_COURSE  WHERE COURSE_ID= '$COURSEID'";

                if (!mysqli_query($condb, $delete)) {
                    die('Error: ' . mysqli_error($condb));
                }
            }
        }
        ?>

        <title></title>
        <meta charset="UTF-8">
    </head>

    <body> 
       

        <div id= "container">
             <div id="menuwrap">   <?php include("../include/header.php"); ?></div>
             
                <?php include("../include/sidepanel.php"); ?>
             
              <div id="cont">
                

                <?php
                $id = $_SESSION['id']; // sets variable to session username
                if (!isset($_SESSION['id'])) {
                    echo "<br/> id is not set  " . $id;
                }

                $condb = mysqli_connect("localhost", "root", "password", "PROJECT"); // creates connection to database


                if (mysqli_connect_errno()) { //checks connection and output error if connection fails
                    echo "connection failed " . mysqli_connect_error();
                }

                $result = mysqli_query($condb, "SELECT COURSE.COURSE_ID,COURSE_NAME FROM PERSON_TAKES_COURSE, USER_INFO, COURSE WHERE PERSON_TAKES_COURSE.ID=USER_INFO.ID AND COURSE.COURSE_ID=PERSON_TAKES_COURSE.COURSE_ID AND  PERSON_TAKES_COURSE.ID='$id';"); // gets the courses related to a specific user;
                ?>
                <div class="wrap">
                    <div class="tablewrap">
                        <h2>Mina kurser</h2>
                        <!--TABLE CONTENT DEPENDS ON USER_ID-->
                        <table>
                            <tr>
                                <th>kurskod</th>
                                <th>kursnamn</th>		
                                <th>Åtgärd</th>
                            </tr>
                            <?php
                            while ($row = mysqli_fetch_array($result)) {
                                $course_id = $row['COURSE_ID'];
                                ?>

                                <tr>
                                    <td><?php echo $row['COURSE_ID'] ?></td>
                                    <td><?php echo $row['COURSE_NAME'] ?></td>		
                                    <td>
                                        <form action="availablecourses.php" method="post">
                                            <!-- hidden field contains values from database used to identify the course to remove-->
                                            <input type="hidden" value="<?php echo $course_id; ?>" name="kurser"></input> 
                                            <input type="submit" value="Ta bort" onclick="return confirm('Vill du verkligen ta bort kursen ?')"/>
                                        </form>
                                    </td>
                                </tr>

                                <?php
                            }
                            ?>


                        </table> 
                    </div>
                </div>

                

                    <?php
                    $result_2 = mysqli_query($condb, "SELECT COURSE.COURSE_ID,COURSE_NAME FROM COURSE;"); // gets the courses related to a specific user;
                    ?>

                <div class="wrap">          
                    <div class="tablewrap"> <!--div used to set box shadow-->
                        <h2>Tillgängliga kurser</h2>

                    <p class="instructions"> Välj lägg till för att lägga till kurser till mina kurser<p>
                        <!--TABLE CONTENT depends on available courses-->
                        <table>
                            <tr>

                                <th>kurskod</th> <!--table header which is a cell-->
                                <th>kursnamn</th>		
                                <th colspan="3">Åtgärd</th> <!--colspan makes cell width size as two-->
                            </tr>
                            <?php
                            while ($row = mysqli_fetch_array($result_2)) {

                                $course_id = $row['COURSE_ID'];
                                ?>
                                <tr>
                                    <td><?php echo $row['COURSE_ID'] ?></td>
                                    <td><?php echo $row['COURSE_NAME'] ?></td>		
                                    <td class="rightborder">
                                        <form action="PersonTakesCourse.php" method="post">

                                            <!-- hidden field contains values from database used to identify the course to remove-->
                                            <input type="hidden" value="<?php echo $course_id; ?>" name="kurser"></input>
                                            <input type="submit" value="Lägg till" onclick="return confirm('Vill du verkligen lägga till kursen ?')"/>
                                        </form>
                                    </td>
                                    <td class="leftborder">
                                        <form action="edit.php" method="post">
                                            <!-- hidden field contains values from database used to identify the course to remove-->
                                            <input type="hidden" value="<?php echo $course_id; ?>" name="course"></input>
                                            <input type="submit" value="Redigera" onclick="return confirm('Vill du redigera kursen ?')" />
                                        </form>
                                    </td>

                                    <td class="leftborder">
                                        <form action="../lectures/lectures.php" method="post">
                                            <!-- hidden field contains values from database used to identify the course to remove-->
                                            <input type="hidden" value="<?php echo $course_id; ?>" name="courseid"></input>
                                            <input type="hidden" value="<?php echo $row['COURSE_NAME']; ?>" name="coursename"></input>
                                            <input type="submit" value="Hantera lektioner" onclick="return confirm('Vill du lägga till lektion?')" />
                                        </form>
                                    </td>
                                </tr>

                                <?php
                            }
                            ?>


                        </table> 
                    </div>
                </div>       
  
                <div class="wrap">
                    
                <h2>Lägga till nya kurser</h2>
                
                <p class="instructions">Fyll i formuläret för att lägga till kurs</p>
                        <form class="frmc" name ="frmc" action="addcourse.php" method="post"><!--form used to add course-->

                            <p>Kurskod: <input type="text" name ="courseId" required></input></p>
                                <!--<input  name = "courseName" required></input></p>-->

                            <p>Startdatum: <input type="date"name = "startDate" required></input></p>

                            <p>Slutdatum: <input type="date"name = "stopDate" required></input></p>
                            <textarea rows="4" cols="25" name = "courseName">Ange kursens namn här!</textarea><br/>
                            <input type="submit" value="spara">
                        </form>  
                    
                </div>
          
        </div>
        </div>
    </body>
